home *** CD-ROM | disk | FTP | other *** search
- Path: newsroom.hitc.com!usenet
- From: psand@eos.hitc.com (G. Patrick Sand)
- Newsgroups: comp.lang.c++
- Subject: Re: [] overload..(newbie in distress)
- Date: 22 Jan 1996 21:11:33 GMT
- Organization: Hughes Aircraft (EOSDIS)
- Message-ID: <4e0ui5$kjn@newsroom.hitc.com>
- References: <DLGppJ.31C@undergrad.math.uwaterloo.ca>
- NNTP-Posting-Host: 155.157.118.56
- Mime-Version: 1.0
- X-Newsreader: WinVN 0.99.3
-
- In article <DLGppJ.31C@undergrad.math.uwaterloo.ca>,
- tthiraku@landen.math.uwaterloo.ca says...
- >
- >Hi..
- >
- >I'm currently stuck on how to overload [] operator such that
- >it does two different tasks..
- >
- >
- >case 1:
- >
- >// A is an instance of a class that contains a linkist.
- >
- > A[5] = val; // store val into the fifth node of a linklist.
- > val = A[5] ; // returns the value of the fifth node of a linklist.
- >
- >
- >I was wondering how can C++ differentiate these two senerios?
- >
- >Anything help will greatly appreciated..
- >
- >Thank You Kindly
- >--
- >Thanou Thirakul
- >tthiraku@undergrad.math.uwaterloo.ca
- >http://www.undergrad.math.uwaterloo.ca/~tthiraku
-
- I think you need the following definitions:
-
- //(T is the value class name, ARRAY is the array class name)
-
- const T ARRAY::operator[]( int i ); // for the x = a[5] operation
- T& ARRAY::operator[]( int i ); // for the a[5] = x operation
-
- Haven't tried it but I bet I am on the right track...Of course, I think
- just defining the second one will work...
-
- oh- be sure to have a well-defined assignment operator (operator=()) for
- the T class if it isn't one of the pre-defined ones (int, float, double,
- etc.)...
-
- Hope this helps...
- --
- G. Patrick Sand
- psand@eos.hitc.com
- PatSand@aol.com
- (301) 925-0791
- "Travel Light But Right..."
- Microsoft Network is prohibited from redistributing
- this work in any form, in whole or in part. License
- to distribute this individual post is available to Microsoft
- for $999. Posting without permission constitutes an
- agreement to these terms...gps
-
-